<template>
	<!-- 完成事件 -->
	<view class="building-add">
		<view style="padding-bottom: 140rpx;">
			<view class="add-content">
				<view class="add-item">
					<view class="item-left">描述<text>*</text></view>
					<view class="item-right"><textarea v-model="description" placeholder-style="color:#999;font-size:26rpx" placeholder="请输入描述" ></textarea></view>
				</view>
			</view>
			<view class="add-head">
				<view class="head-left">
					<view class="head-left-1">上传图片</view>
					<view class="head-left-2">图片建议大小不超过2M</view>
				</view>
				<view class="head-right" @click="chooseImage">
					<image class="img" v-if="imgs==''" src="../../../static/icon/jia-.png"></image>
					<image class="img2" v-else :src="imgs"></image>
				</view>
			</view>
			<view class="video">上传视频</view>
			<view class="upload-content">
				<view class="con-1" v-if="flag"><image @click="chooseVideo" src="../../../static/icon/jia-.png"></image></view>
				<view class="con-2" v-else>
					<video :controls="false" :loop="true" :autoplay="true" :show-center-play-btn="false" show-fullscreen-btn="false" :src="videoUrl"></video>
				</view>
			</view>
			<view class="video">上传音频</view>
			<view>
			    <button @tap="startRecord">开始录音</button>
			    <button @tap="endRecord">停止录音</button>
			    <button @tap="playVoice">播放录音</button>
			</view>
		</view>
		
		 
		
		<view class="btn">
			<view class="btn-1" :class="btnFlag ? 'act2' : 'act1' " @click="handleSubmit">保存</view>
		</view>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();
	
	innerAudioContext.autoplay = true;
	
	export default{
		data() {
			return{
				
				text: 'uni-app',
				voicePath: '',
				
				eventId:'',
				imgs:'',
				description:'',
				videoUrl:'',
				src:'',
				voiceUrl:'',
				btnFlag:true,
				flag:true,
				cameraList: [{
						value: 'back',
						name: '后置摄像头',
						checked: 'true'
					},
					{
						value: 'front',
						name: '前置摄像头'
					},
				],
				cameraIndex: 0,
			}
		},
		mounted() {
			
		},
		onLoad(options) {
			this.eventId = options.id
			// 音频
			let self = this;
			recorderManager.onStop(function (res) {
			    console.log('recorder stop' + JSON.stringify(res));
			    self.voicePath = res.tempFilePath;
				uni.uploadFile({
					url: self.$u.http.config.uplodeUrl,
					filePath: res.tempFilePath,
					name: 'file',
					success: (res) => {
						// console.log(JSON.parse(res.data))
						self.voiceUrl = JSON.parse(res.data).message
						uni.showToast({
							title:'音频上传成功',
							icon:'success'
						})
					},
					fail: (res) => {
						console.log(res)
						uni.showToast({
							icon:'none',
							title:'音频上传失败'
						})
					}
				})
			});
		},
		methods:{
			
			        startRecord() {
			            console.log('开始录音');
			
			            recorderManager.start();
			        },
			        endRecord() {
			            console.log('录音结束');
			            recorderManager.stop();
			        },
			        playVoice() {
			            console.log('播放录音');
			
			            if (this.voicePath) {
			                innerAudioContext.src = this.voicePath;
			                innerAudioContext.play();
			            }
			        },
			
			// 上传图片
			chooseImage() {
				const self = this
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// console.log(res.tempFilePaths[0])
						uni.uploadFile({
							url: self.$u.http.config.uplodeUrl,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (res) => {
								// console.log(JSON.parse(res.data))
								self.imgs = JSON.parse(res.data).message
							},
							fail: (res) => {
								console.log(res)
								uni.showToast({
									icon:'none',
									title:'上传失败'
								})
							}
						})
					}
				})
			},
			// 上传视频
			chooseVideo() {
				// 上传视频
				uni.chooseVideo({
					maxDuration:60,
					count: 1,
					camera: this.cameraList[this.cameraIndex].value,
					sourceType: ['album'],
					success: (responent) => {
						if(responent.tempFilePath.length != 0) {
							this.flag = false
						} else {
							this.flag = true
						}
						this.videoUrl = responent.tempFilePath
						// console.log(responent)
						uni.showLoading({
						    title: '视频正在上传'
						});
						uni.uploadFile({
							url: this.$u.http.config.uplodeUrl,
							method:"POST",
							filePath:this.videoUrl,
							name:'file',
							// formData: {
							//    biz: 'temp'
							// },
							success: (res) => {
								// console.log(res)
								uni.hideLoading()
								uni.showToast({
								    title: '视频上传成功',
								    duration: 1000
								});
								console.log(res)
								this.src = JSON.parse(res.data).message
								this.btnFlag = false
							},
							fail:res => {
								// console.log(res)
							}
						})
					},
					fail(err) {
						// console.log(err)
					}
				})
			},
			// 完成
			handleSubmit() {
				if(this.description == '') {
					return this.$refs.uToast.show({
						title: '请填写描述'
					})
				}
				if(this.imgs == '') {
					return this.$refs.uToast.show({
						title: '请上传图片'
					})
				}
				if(this.src == '') {
					return this.$refs.uToast.show({
						title: '请上传视频'
					})
				}
				if(this.voiceUrl == '') {
					return this.$refs.uToast.show({
						title: '请上传音频'
					})
				}
				if(this.btnFlag) {
					return this.$refs.uToast.show({
						title: '请等待视频上传完成'
					})
				}
				let params = {
					eventId:this.eventId,
					imgs:this.imgs,
					description:this.description,
					videoUrl:this.videoUrl,
					voiceUrl:this.voiceUrl
				}
				console.log(params)
				this.$u.api.finishEvent(params)
					.then(res => {
						console.log(res)
						if(res.code == 200) {
							uni.showToast({
								title:'操作成功',
								icon:'success'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta:1
								})
							},1500)
						}
					})
					.catch(err => {
						console.log(err)
						uni.showToast({
							title:'操作失败',
							icon:'none'
						})
					})
			}
		}

	}
</script>

<style lang="scss" scoped>
	.act1{
		background-color: #007AFF;
		color: #fff;
	}
	.act2{
		background-color: #999;
		color: #fff;
	}
	.building-add{
		background-color: #f5f6f7;
		min-height: 100vh;
		.gridcontent{
			height: 40vh;
			width: 100%;
			padding: 20rpx;
			color: #333;
			font-size: 27rpx;
			text-align: left;
			.item{
				width: 100%;
				padding: 5rpx 40rpx;
				overflow-y: auto;
				.item1{
					padding: 5rpx 40rpx;
					.item2{
						padding: 5rpx 40rpx;
						.item3{
							padding: 5rpx 40rpx;
							.item4{
								padding: 5rpx 40rpx;
								
							}
						}
					}
				}
			}
		}
		.video{
			padding: 20rpx;
			color: #000;
			font-size: 28rpx;
			font-weight: 600;
		}
		.upload-content{
			
			.con-1{
				margin: 30rpx auto;
				width: 85%;
				height: 700rpx;
				border-radius: 10rpx;
				border: 1px solid #f4f1f1;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #c4c4c4;
				image{
					width: 100rpx;
					height: 100rpx;
					
				}
			}
			.con-2{
				margin: 30rpx auto;
				width: 85%;
				height: 700rpx;
				video{
					border-radius: 10rpx;
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-head{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			height: 180rpx;
			padding: 20rpx;
			.head-left{
				.head-left-1{
					color: #000;
					font-size: 28rpx;
					font-weight: 600;
				}
				.head-left-2{
					color: #999;
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
			.head-right{
				background-color: #cecece;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 10rpx;
				width: 150rpx;
				height: 150rpx;
				.img{
					width: 50%;
					height: 50%;
				}
				.img2{
					width: 100%;
					height: 100%;
				}
			}
		}
		.add-content{
			margin-top: 20rpx;
			.add-item{
				display: flex;
				// justify-content: space-between;
				align-items: flex-start;
				padding: 30rpx 20rpx;
				background-color: #fff;
				border-bottom: 1px solid #f4f1f1;
				.item-left{
					color: #333;
					font-size: 28rpx;
					flex: 2;
					text{
						color: #DD524D;
					}
				}
				.item-right{
					font-size: 27rpx;
					color: #999;
					display: flex;
					justify-content: flex-end;
					align-items: center;
					flex: 4;
					input{
						text-align: right;
					}
					input::placeholder{
					    font-size: 26rpx;
					    color: #999;
					}
					textarea{
						
					}
					text{
						display: inline-block;
						margin-left: 5rpx;
					}
				}
			}
		}
		.btn{
			width: 100%;
			height: 110rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 30rpx;
			background-color: #f4f1f1;
			position: fixed;
			bottom: 0;
			left: 0;
			border-top: 1px solid #CCE6FF;
			z-index: 100;
			.btn-1{
				display: flex;
				justify-content: center;
				align-items: center;
				width: 600rpx;
				height: 80rpx;
				border-radius: 40rpx;
			}
			.btn-1:active{
				background-color: #CCE6FF;
			}
		}
	}
</style>
